home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / PROGRAMMING / PERL.SPK / Perl5001 / Manual / perlcall_h < prev    next >
Text File  |  1995-04-18  |  27KB  |  844 lines

  1. <!-- $RCSfile$$Revision$$Date$ -->
  2. <!-- $Log$ -->
  3. <HTML>
  4. <TITLE> PERLCALL </TITLE>
  5. <h2>NAME</h2>
  6. perlcall - Perl calling conventions from C
  7. <p><h2>DESCRIPTION</h2>
  8. <B>WARNING : This document is still under construction. 
  9. There are bound to be a number of inaccuracies, so tread very carefully for now.</B>
  10. <p>The purpose of this document is to show you how to write <I>callbacks</I>, 
  11. i.e. how to call Perl from C. The main
  12. focus is on how to interface back to Perl from a bit of C code that has itself
  13. been run by Perl, i.e. the 'main' program is a Perl script; you are using it
  14. to execute
  15. a section of code written in C; that bit of C code wants you to do something
  16. with a particular event, so you want a Perl sub to be executed whenever it
  17. happens.
  18. <p>Examples where this is necessary include
  19. <p>
  20. <dl>
  21. <dt><B>* </B>
  22. <dd>
  23. You have created an XSUB interface to an application's C API.
  24. <p></dd>
  25. A fairly common feature in applications is to allow you to define a C
  26. function that will get called whenever something nasty occurs.
  27. What we would like is for a Perl sub to be called instead.
  28. <p><dt><B>*</B>
  29. <dd>
  30. The classic example of where callbacks are used is in an event driven program 
  31. like for X-windows.
  32. In this case your register functions to be called whenever a specific events
  33. occur, e.g. a mouse button is pressed.
  34. <p></dd>
  35.  
  36. </dl>
  37.  
  38. Although the techniques described are applicable to embedding Perl
  39. in a C program, this is not the primary goal of this document. For details
  40. on embedding Perl in C refer to 
  41. <A HREF="perlembed.html">
  42. the perlembed manpage</A>
  43.  (currently unwritten).
  44. <p>Before you launch yourself head first into the rest of this document, it would 
  45. be a good idea to have read the following two documents - 
  46. <A HREF="perlapi.html">
  47. the perlapi manpage</A>
  48.  and 
  49. <A HREF="perlguts.html">
  50. the perlguts manpage</A>
  51. .
  52. <p>This stuff is easier to explain using examples. But first here are a few
  53. definitions anyway.
  54. <p><h3>Definitions</h3>
  55. Perl has a number of C functions which allow you to call Perl subs. They are
  56. <p><pre>
  57.         I32 perl_call_sv(SV* sv, I32 flags) ;
  58.         I32 perl_call_pv(char *subname, I32 flags) ;
  59.         I32 perl_call_method(char *methname, I32 flags) ;
  60.         I32 perl_call_argv(char *subname, I32 flags, register char **argv) ;
  61. </pre>
  62. The key function is <I>perl_call_sv</I>. All the other functions make use of
  63. <I>perl_call_sv</I> to do what they do.
  64. <p><I>perl_call_sv</I> takes two parameters, the first is an SV*. This allows you to 
  65. specify the Perl sub to be called either as a C string (which has first been 
  66. converted to an SV) or a reference to a 
  67. sub. Example 7, shows you how you can make use of <I>perl_call_sv</I>.
  68. The second parameter, <B>flags</B>, is a general purpose option command. 
  69. This parameter is common to all the <I>perl_call_*</I> functions. 
  70. It is discussed in the next section.
  71. <p>The function, <I>perl_call_pv</I>, is similar as <I>perl_call_sv</I> except it 
  72. expects it's first parameter has to be a C char* which identifies the Perl 
  73. sub you want to call, e.g. <B>perl_call_pv("fred", 0)</B>.
  74. <p>The function <I>perl_call_method</I> expects its first argument to contain a 
  75. blessed reference to a class. Using that reference it looks up and calls <B>methname</B> 
  76. from that class. See example 9.
  77. <p><I>perl_call_argv</I> calls the Perl sub specified by the <B>subname</B> parameter. 
  78. It also takes the usual <B>flags</B> parameter. 
  79. The final parameter, <B>argv</B>, consists of a 
  80. list of C strings to be sent to the Perl sub. See example 8.
  81. <p>All the functions return a number. This is a count of the number of items
  82. returned by the Perl sub on the stack.
  83. <p>As a general rule you should <I>always</I> check the return value from these 
  84. functions.
  85. Even if you are only expecting a particular number of values to be returned 
  86. from the Perl sub, there is nothing to stop someone from doing something
  87. unexpected - don't say you havn't been warned.
  88. <p><h3>Flag Values</h3>
  89. The <B>flags</B> parameter in all the <I>perl_call_*</I> functions consists of any 
  90. combination of the symbols defined below, OR'ed together.
  91. <p>
  92. <dl>
  93.  
  94. <dt><b><A NAME="perlcall_42">G_SCALAR    </A></b>
  95. <dd>
  96. Calls the Perl sub in a scalar context.
  97. <p></dd>
  98. Whatever the Perl sub actually returns, we only want a scalar. If the perl sub 
  99. does return a scalar, the return value from the <I>perl_call_*</I> function 
  100. will be 1 or 0. If 1, then the value actually returned by the Perl sub will 
  101. be contained
  102. on the top of the stack. 
  103. If 0, then the sub has probably called 
  104. <A HREF="perlfunc.html#perlfunc_96">die</A>
  105.  or you have 
  106. used the G_DISCARD flag.
  107. <p>If the Perl sub returns a list, the <I>perl_call_*</I> function will still
  108. only return 1 or 0. If 1, then the number of elements in the list 
  109. will be stored on top of the stack.
  110. The actual values of the list will not be accessable. 
  111. <p>G_SCALAR is the default flag setting for all the functions.
  112. <p>
  113. <dt><b><A NAME="perlcall_43">G_ARRAY    </A></b>
  114. <dd>
  115. Calls the Perl sub in a list context.
  116. <p></dd>
  117. The return code from the <I>perl_call_*</I> functions will indicate how
  118. many elements of the stack are used to store the array.
  119. <p>
  120. <dt><b><A NAME="perlcall_44">G_DISCARD    </A></b>
  121. <dd>
  122. If you are not interested in the values returned by the Perl sub then setting
  123. this flag will make Perl get rid of them automatically for you. This will take
  124. precedence to either G_SCALAR or G_ARRAY.
  125. <p></dd>
  126. If you do 
  127. not set this flag then you may need to explicitly get rid of temporary values.
  128. See example 3 for details.
  129. <p>
  130. <dt><b><A NAME="perlcall_45">G_NOARGS    </A></b>
  131. <dd>
  132. If you are not passing any parameters to the Perl sub, you can save a bit of 
  133. time by setting this flag. It has the effect of of not creating the <B>@_</B> array 
  134. for the Perl sub.
  135. <p></dd>
  136. A point worth noting is that if this flag is specified the Perl sub called can 
  137. still access an <B>@_</B> array from a previous Perl sub. 
  138. This functionality can be illustrated with the perl code below
  139. <p><pre>
  140.         sub fred
  141.           { print "@_\n"  }
  142. </pre>
  143. <pre>
  144.         sub joe
  145.           { &fred }
  146. </pre>
  147. <pre>
  148.         &joe(1,2,3) ;
  149. </pre>
  150. This will print
  151. <p><pre>
  152.         1 2 3
  153. </pre>
  154. What has happened is that <B>fred</B> accesses the <B>@_</B> array which belongs to <B>joe</B>.
  155. <p>
  156. <dt><b><A NAME="perlcall_46">G_EVAL    </A></b>
  157. <dd>
  158. If the Perl sub you are calling has the ability to terminate 
  159. abnormally, e.g. by calling 
  160. <A HREF="perlfunc.html#perlfunc_96">die</A>
  161.  or by not actually existing, and 
  162. you want to catch this type of event, specify this flag setting. It will put 
  163. an <I>eval { }</I> around the sub call.
  164. <p></dd>
  165. Whenever control returns from the <I>perl_call_*</I> function you need to
  166. check the 
  167. <A HREF="perlvar.html#perlvar_438">$@</A>
  168.  variable as you would in a normal Perl script. 
  169. See example 6 for details of how to do this.
  170. <p>
  171. </dl>
  172.  
  173. <h2>EXAMPLES</h2>
  174. Enough of the definition talk, let's have a few examples.
  175. <p>Perl provides many macros to assist in accessing the Perl stack. 
  176. These macros should always be used when interfacing to Perl internals.
  177. Hopefully this should make the code less vulnerable to changes made to
  178. Perl in the future.
  179. <p>Another point worth noting is that in the first series of examples I have 
  180. only made use of the <I>perl_call_pv</I> function. 
  181. This has only been done to ease you into the 
  182. topic. Wherever possible, if the choice is between using <I>perl_call_pv</I> 
  183. and <I>perl_call_sv</I>, I would always try to use <I>perl_call_sv</I>.
  184. <p>The code for these examples is stored in the file <I>perlcall.tar</I>. 
  185. (Once this document settles down, all the example code will be available in the file).
  186. <p><h3>Example1: No Parameters, Nothing returned</h3>
  187. This first trivial example will call a Perl sub, <I>PrintUID</I>, to print 
  188. out the UID of the process. 
  189. <p><pre>
  190.         sub PrintUID
  191.         {
  192.             print "UID is $<\n" ;
  193.         }
  194. </pre>
  195. and here is the C to call it
  196. <p><pre>
  197.         void
  198.         call_PrintUID()
  199.         {
  200.         dSP ;
  201. </pre>
  202. <pre>
  203.         PUSHMARK(sp) ;
  204.             perl_call_pv("PrintUID", G_DISCARD|G_NOARGS) ;
  205.         }
  206. </pre>
  207. Simple, eh. 
  208. <p>A few points to note about this example. 
  209. <p>
  210. <dl>
  211.  
  212. <dt><b><A NAME="perlcall_48">1. </A></b>
  213. <dd>
  214. We aren't passing any parameters to <I>PrintUID</I> so G_NOARGS
  215. can be specified.
  216. <p></dd>
  217.  
  218. <dt><b><A NAME="perlcall_49">2.</A></b>
  219. <dd>
  220. Ignore <B>dSP</B> and <B>PUSHMARK(sp)</B> for now. They will be discussed in the next
  221. example.
  222. <p></dd>
  223.  
  224. <dt><b><A NAME="perlcall_50">3. </A></b>
  225. <dd>
  226. We aren't interested in anything returned from <I>PrintUID</I>, so
  227. G_DISCARD is specified. Even if <I>PrintUID</I> was changed to actually
  228. return some value(s), having specified G_DISCARD will mean that they
  229. will be wiped by the time control returns from <I>perl_call_pv</I>.
  230. <p></dd>
  231.  
  232. <dt><b><A NAME="perlcall_51">4. </A></b>
  233. <dd>
  234. Because we specified G_DISCARD, it is not necessary to check 
  235. the value returned from <I>perl_call_sv</I>. It will always be 0.
  236. <p></dd>
  237.  
  238. <dt><b><A NAME="perlcall_52">5.</A></b>
  239. <dd>
  240. As <I>perl_call_pv</I> is being used, the Perl sub is specified as a C string.
  241. <p></dd>
  242.  
  243. </dl>
  244.  
  245. <h3>Example 2: Passing Parameters</h3>
  246. Now let's make a slightly more complex example. This time we want 
  247. to call a Perl sub
  248. which will take 2 parameters - a string (<B>$s</B>) and an integer (<B>$n</B>). 
  249. The sub will simply print the first <B>$n</B> characters of the string.
  250. <p>So the Perl sub would look like this
  251. <p><pre>
  252.         sub LeftString
  253.         {
  254.             my($s, $n) = @_ ;
  255.             print substr($s, 0, $n), "\n" ;
  256.         }
  257. </pre>
  258. The C function required to call <I>LeftString</I> would look like this.
  259. <p><pre>
  260.         static void
  261.         call_LeftString(a, b)
  262.         char * a ;
  263.         int b ;
  264.         {
  265.             dSP ;
  266. </pre>
  267. <pre>
  268.             PUSHMARK(sp) ;
  269.             XPUSHs(sv_2mortal(newSVpv(a, 0)));
  270.             XPUSHs(sv_2mortal(newSViv(b)));
  271.             PUTBACK ;
  272. </pre>
  273. <pre>
  274.             perl_call_pv("LeftString", G_DISCARD);
  275.         }
  276. </pre>
  277. Here are a few notes on the C function <I>call_LeftString</I>.
  278. <p>
  279. <dl>
  280.  
  281. <dt><b><A NAME="perlcall_48">1. </A></b>
  282. <dd>
  283. The only flag specified this time is G_DISCARD. As we are passing 2 
  284. parameters to the Perl sub this time, we have not specified G_NOARGS.
  285. <p></dd>
  286.  
  287. <dt><b><A NAME="perlcall_49">2. </A></b>
  288. <dd>
  289. Parameters are passed to the Perl sub using the Perl stack.
  290. This is the purpose of the code beginning with the line <B>dSP</B> and ending
  291. with the line <B>PUTBACK</B>.
  292. <p></dd>
  293.  
  294. <dt><b><A NAME="perlcall_50">3.</A></b>
  295. <dd>
  296. If you are going to put something onto the Perl stack, you need to know
  297. where to put it. This is the purpose of the macro <B>dSP</B> -
  298. it declares and initialises a local copy of the Perl stack pointer.
  299. <p></dd>
  300. All the other macros which will be used in this example require you to
  301. have used this macro. 
  302. <p>If you are calling a Perl sub directly from an XSUB function, it is 
  303. not necessary to explicitly use the <B>dSP</B> macro - it will be declared for you.
  304. <p>
  305. <dt><b><A NAME="perlcall_51">4.</A></b>
  306. <dd>
  307. Any parameters to be pushed onto the stack should be bracketed by the
  308. <B>PUSHMARK</B> and <B>PUTBACK</B> macros. 
  309. The purpose of these two macros, in this context, is to automatically count
  310. the number of parameters you are pushing. Then whenever Perl is creating
  311. the <B>@_</B> array for the sub, it knows how big to make it.
  312. <p></dd>
  313. The <B>PUSHMARK</B> macro tells Perl to make a mental note of the current stack
  314. pointer. Even if you aren't passing any parameters (like in Example 1) you must 
  315. still call the <B>PUSHMARK</B> macro before you can call any of 
  316. the <I>perl_call_*</I> functions - Perl still needs to know that there are 
  317. no parameters.
  318. <p>The <B>PUTBACK</B> macro sets the global copy of the stack pointer to be the
  319. same as our local copy. If we didn't do this <I>perl_call_pv</I> wouldn't
  320. know where the two parameters we pushed were - remember that up to now
  321. all the stack pointer manipulation we have done is with our local copy,
  322. <I>not</I> the global copy.
  323. <p>
  324. <dt><b><A NAME="perlcall_52">5.</A></b>
  325. <dd>
  326. Next, we come to XPUSHs. This is where the parameters actually get
  327. pushed onto the stack. In this case we are pushing a string and an integer.
  328. <p></dd>
  329. See the section <I>XSUB's AND  THE ARGUMENT STACK</I> in 
  330. <A HREF="perlguts.html">
  331. the perlguts manpage</A>
  332.  for
  333. details on how the XPUSH macros work.
  334. <p>
  335. <dt><b><A NAME="perlcall_54">6.</A></b>
  336. <dd>
  337. Finally, <I>LeftString</I> can now be called via the <I>perl_call_pv</I> function.
  338. <p></dd>
  339.  
  340. </dl>
  341.  
  342. <h3>Example 3: Returning a Scalar</h3>
  343. Now for an example of dealing with the values returned from a Perl sub.
  344. <p>Here is a Perl sub, <I>Adder</I>,  which takes 2 integer parameters and simply 
  345. returns their sum.
  346. <p><pre>
  347.         sub Adder
  348.         {
  349.             my($a, $b) = @_ ;
  350.             $a + $b ;
  351.         }
  352. </pre>
  353. As we are now concerned with the return value from <I>Adder</I>, the C function
  354. is now a bit more complex.
  355. <p><pre>
  356.         static void
  357.         call_Adder(a, b)
  358.         int a ;
  359.         int b ;
  360.         {
  361.             dSP ;
  362.             int count ;
  363. </pre>
  364. <pre>
  365.             ENTER ;
  366.             SAVETMPS;
  367. </pre>
  368. <pre>
  369.             PUSHMARK(sp) ;
  370.             XPUSHs(sv_2mortal(newSViv(a)));
  371.             XPUSHs(sv_2mortal(newSViv(b)));
  372.             PUTBACK ;
  373. </pre>
  374. <pre>
  375.             count = perl_call_pv("Adder", G_SCALAR);
  376. </pre>
  377. <pre>
  378.             SPAGAIN ;
  379. </pre>
  380. <pre>
  381.         if (count != 1)
  382.             croak("Big trouble\n") ;
  383. </pre>
  384. <pre>
  385.         printf ("The sum of %d and %d is %d\n", a, b, POPi) ;
  386. </pre>
  387. <pre>
  388.             PUTBACK ;
  389.             FREETMPS ;
  390.             LEAVE ;
  391.         }
  392. </pre>
  393. Points to note this time are
  394. <p>
  395. <dl>
  396.  
  397. <dt><b><A NAME="perlcall_48">1. </A></b>
  398. <dd>
  399. The only flag specified this time was G_SCALAR. That means the @_ array
  400. will be created and that the value returned by <I>Adder</I> will still
  401. exist after the call to <I>perl_call_pv</I>.
  402. <p></dd>
  403.  
  404. <dt><b><A NAME="perlcall_49">2.</A></b>
  405. <dd>
  406. Because we are interested in what is returned from <I>Adder</I> we cannot specify
  407. G_DISCARD. This means that we will have to tidy up the Perl stack and dispose
  408. of any temporary values ourselves. This is the purpose of 
  409. <p></dd>
  410. <pre>
  411.         ENTER ;
  412.         SAVETMPS ;
  413. </pre>
  414. at the start of the function, and
  415. <p><pre>
  416.         FREETMPS ;
  417.         LEAVE ;
  418. </pre>
  419. at the end. The <B>ENTER</B>/<B>SAVETMPS</B> pair creates a boundary for any 
  420. temporaries we create. 
  421. This means that the temporaries we get rid of will be limited to those which
  422. were created after these calls.
  423. <p>The <B>FREETMPS</B>/<B>LEAVE</B> pair will get rid of any values returned by the Perl 
  424. sub, plus it will also dump the mortal SV's we created. 
  425. Having <B>ENTER</B>/<B>SAVETMPS</B> at the beginning
  426. of the code makes sure that no other mortals are destroyed.
  427. <p>
  428. <dt><b><A NAME="perlcall_50">3.</A></b>
  429. <dd>
  430. The purpose of the macro <B>SPAGAIN</B> is to refresh the local copy of the
  431. stack pointer. This is necessary because it is possible that the memory
  432. allocated to the Perl stack has been re-allocated whilst in the <I>perl_call_pv</I>
  433. call.
  434. <p></dd>
  435. If you are making use of the Perl stack pointer in your code you must always
  436. refresh the your local copy using SPAGAIN whenever you make use of
  437. of the <I>perl_call_*</I> functions or any other Perl internal function.
  438. <p>
  439. <dt><b><A NAME="perlcall_51">4. </A></b>
  440. <dd>
  441. Although only a single value was expected to be returned from <I>Adder</I>, it is
  442. still good practice to check the return code from <I>perl_call_pv</I> anyway.
  443. <p></dd>
  444. Expecting a single value is not quite the same as knowing that there will
  445. be one. If someone modified <I>Adder</I> to return a list and we didn't check
  446. for that possibility and take appropriate action the Perl stack would end 
  447. up in an inconsistant state. That is something you <I>really</I> don't want
  448. to ever happen.
  449. <p>
  450. <dt><b><A NAME="perlcall_52">5.</A></b>
  451. <dd>
  452. The <B>POPi</B> macro is used here to pop the return value from the stack. In this
  453. case we wanted an integer, so <B>POPi</B> was used.
  454. <p></dd>
  455. Here is the complete list of POP macros available, along with the types they 
  456. return.
  457. <p>
  458. <listing>
  459.     POPs    SV 
  460.     POPp    pointer 
  461.     POPn    double 
  462.     POPi    integer 
  463.     POPl    long 
  464. </listing>
  465.  
  466. <dt><b><A NAME="perlcall_54">6.</A></b>
  467. <dd>
  468. The final <B>PUTBACK</B> is used to leave the Perl stack in a consistant state 
  469. before exiting the function. This is
  470. necessary because when we popped the return value from the stack with <B>POPi</B> it
  471. only updated our local copy of the stack pointer. Remember, <B>PUTBACK</B> sets the
  472. global stack pointer to be the same as our local copy.
  473. <p></dd>
  474.  
  475. </dl>
  476.  
  477. <h3>Example 4: Returning a list of values</h3>
  478. Now, let's extend the previous example to return both the sum of the parameters 
  479. and the difference.
  480. <p>Here is the Perl sub
  481. <p><pre>
  482.         sub AddSubtract
  483.         {
  484.            my($a, $b) = @_ ;
  485.            ($a+$b, $a-$b) ;
  486.         }
  487. </pre>
  488. and this is the C function
  489. <p><pre>
  490.         static void
  491.         call_AddSubtract(a, b)
  492.         int a ;
  493.         int b ;
  494.         {
  495.             dSP ;
  496.             int count ;
  497. </pre>
  498. <pre>
  499.             ENTER ;
  500.             SAVETMPS;
  501. </pre>
  502. <pre>
  503.             PUSHMARK(sp) ;
  504.             XPUSHs(sv_2mortal(newSViv(a)));
  505.             XPUSHs(sv_2mortal(newSViv(b)));
  506.             PUTBACK ;
  507. </pre>
  508. <pre>
  509.             count = perl_call_pv("AddSubtract", G_ARRAY);
  510. </pre>
  511. <pre>
  512.             SPAGAIN ;
  513. </pre>
  514. <pre>
  515.         if (count != 2)
  516.             croak("Big trouble\n") ;
  517. </pre>
  518. <pre>
  519.         printf ("%d - %d = %d\n", a, b, POPi) ;
  520.         printf ("%d + %d = %d\n", a, b, POPi) ;
  521. </pre>
  522. <pre>
  523.             PUTBACK ;
  524.             FREETMPS ;
  525.             LEAVE ;
  526.         }
  527. </pre>
  528. Notes
  529. <p>
  530. <dl>
  531.  
  532. <dt><b><A NAME="perlcall_48">1.</A></b>
  533. <dd>
  534. We wanted array context, so we used G_ARRAY.
  535. <p></dd>
  536.  
  537. <dt><b><A NAME="perlcall_49">2.</A></b>
  538. <dd>
  539. Not surprisingly there are 2 POPi's this time  because we were retrieving 2
  540. values from the stack. The main point to note is that they came off the stack in
  541. reverse order.
  542. <p></dd>
  543.  
  544. </dl>
  545.  
  546. <h3>Example 5: Returning Data from Perl via the parameter list</h3>
  547. It is also possible to return values directly via the parameter list -
  548. whether it is actually desirable to do it is another matter entirely.
  549. <p>The Perl sub, <I>Inc</I>, below takes 2 parameters and increments each.
  550. <p><pre>
  551.         sub Inc
  552.         {
  553.             ++ $_[0] ;
  554.             ++ $_[1] ;
  555.         }
  556. </pre>
  557. and here is a C function to call it.
  558. <p><pre>
  559.         static void
  560.         call_Inc(a, b)
  561.         int a ;
  562.         int b ;
  563.         {
  564.             dSP ;
  565.             int count ;
  566.             SV * sva ;
  567.             SV * svb ;
  568. </pre>
  569. <pre>
  570.             ENTER ;
  571.             SAVETMPS;
  572. </pre>
  573. <pre>
  574.             sva = sv_2mortal(newSViv(a)) ;
  575.             svb = sv_2mortal(newSViv(b)) ;
  576. </pre>
  577. <pre>
  578.             PUSHMARK(sp) ;
  579.             XPUSHs(sva);
  580.             XPUSHs(svb);
  581.             PUTBACK ;
  582. </pre>
  583. <pre>
  584.             count = perl_call_pv("Inc", G_DISCARD);
  585. </pre>
  586. <pre>
  587.             if (count != 0)
  588.                 croak ("call_Inc : expected 0 return value from 'Inc', got %d\n", count) ;
  589. </pre>
  590. <pre>
  591.             printf ("%d + 1 = %d\n", a, SvIV(sva)) ;
  592.             printf ("%d + 1 = %d\n", b, SvIV(svb)) ;
  593. </pre>
  594.  
  595. <listing>
  596.         FREETMPS ; 
  597.     LEAVE ;  
  598.     } 
  599. </listing>
  600. To be able to access the two parameters that were pushed onto the stack 
  601. after they return from <I>perl_call_pv</I> it is necessary to make a note of
  602. their addresses - thus the two variables <B>sva</B> and <B>svb</B>. 
  603. <p>The reason this is necessary is that
  604. the area of the Perl stack which held them
  605. will very likely have been overwritten by something else by the time control
  606. returns from <I>perl_call_pv</I>.
  607. <p><h3>Example 6: Using G_EVAL</h3>
  608. Now an example using G_EVAL. Below is a Perl sub which computes the 
  609. difference of its 2 parameters. If this would result in a negative result,
  610. the sub calls 
  611. <A HREF="perlfunc.html#perlfunc_96">die</A>
  612. .
  613. <p><pre>
  614.         sub Subtract
  615.         {
  616.         my ($a, $b) = @_ ;
  617. </pre>
  618. <pre>
  619.             die "death can be fatal\n" if $a < $b ;
  620. </pre>
  621. <pre>
  622.         $a - $b ;
  623.         }
  624. </pre>
  625. and some C to call it
  626. <p><pre>
  627.         static void
  628.         call_Subtract(a, b)
  629.         int a ;
  630.         int b ;
  631.         {
  632.             dSP ;
  633.             int count ;
  634.         SV * sv ;
  635. </pre>
  636. <pre>
  637.             ENTER ;
  638.             SAVETMPS;
  639. </pre>
  640. <pre>
  641.             PUSHMARK(sp) ;
  642.             XPUSHs(sv_2mortal(newSViv(a)));
  643.             XPUSHs(sv_2mortal(newSViv(b)));
  644.             PUTBACK ;
  645. </pre>
  646. <pre>
  647.             count = perl_call_pv("Subtract", G_EVAL|G_SCALAR);
  648. </pre>
  649. <pre>
  650.         /* Check the eval first */
  651.             sv = GvSV(gv_fetchpv("@", TRUE, SVt_PV));
  652.             if (SvTRUE(sv))
  653.                 printf ("Uh oh - %s\n", SvPV(sv, na)) ;
  654. </pre>
  655. <pre>
  656.             SPAGAIN ;
  657. </pre>
  658. <pre>
  659.             if (count != 1)
  660.                 croak ("call_Subtract : expected 1 return value from 'Subtract', got %d\n", count) ;
  661. </pre>
  662. <pre>
  663.         printf ("%d - %d = %d\n", a, b, POPi) ;
  664. </pre>
  665. <pre>
  666.             PUTBACK ;
  667.             FREETMPS ;
  668.             LEAVE ;
  669. </pre>
  670. <pre>
  671.         }
  672. </pre>
  673. If <I>call_Subtract</I> is called thus
  674. <p><pre>
  675.         call_Subtract(4, 5)
  676. </pre>
  677. the following will be printed
  678. <p><pre>
  679.         Uh oh - death can be fatal
  680. </pre>
  681. Notes
  682. <p>
  683. <dl>
  684.  
  685. <dt><b><A NAME="perlcall_48">1.</A></b>
  686. <dd>
  687. We want to be able to catch the 
  688. <A HREF="perlfunc.html#perlfunc_96">die</A>
  689.  so we have used the G_EVAL flag.
  690. Not specifying this flag would mean that the program would terminate.
  691. <p></dd>
  692.  
  693. <dt><b><A NAME="perlcall_49">2.</A></b>
  694. <dd>
  695. The code 
  696. <p></dd>
  697. <pre>
  698.             sv = GvSV(gv_fetchpv("@", TRUE, SVt_PV));
  699.             if (SvTRUE(sv))
  700.                 printf ("Uh oh - %s\n", SvPVx(sv, na)) ;
  701. </pre>
  702. is the equivalent of this bit of Perl
  703. <p><pre>
  704.         print "Uh oh - $@\n" if $@ ;
  705. </pre>
  706.  
  707. </dl>
  708.  
  709. <h3>Example 7: Using perl_call_sv</h3>
  710. In all the previous examples I have 'hard-wried' the name of the Perl sub to
  711. be called from C. 
  712. Sometimes though, it is necessary to be able to specify the name
  713. of the Perl sub from within the Perl script.
  714. <p>Consider the Perl code below
  715. <p><pre>
  716.         sub fred
  717.         {
  718.             print "Hello there\n" ;
  719.         }
  720. </pre>
  721. <pre>
  722.         CallSub("fred") ;
  723. </pre>
  724. here is a snippet of XSUB which defines <I>CallSub</I>.
  725. <p><pre>
  726.         void
  727.         CallSub(name)
  728.                 char *  name
  729.                 CODE:
  730.                 PUSHMARK(sp) ;
  731.                 perl_call_pv(name, G_DISCARD|G_NOARGS) ;
  732. </pre>
  733. That is fine as far as it goes. The thing is, it only allows the Perl sub to be
  734. specified as a string. 
  735. For perl 4 this was adequate, but Perl 5 allows references to 
  736. subs and anonymous subs. This is where <I>perl_call_sv</I> is useful.
  737. <p>The code below for <I>CallSub</I> is identical to the previous time except that the
  738. <B>name</B> parameter is now defined as an SV* and we use <I>perl_call_sv</I> instead of
  739. <I>perl_call_pv</I>.
  740. <p><pre>
  741.         void
  742.         CallSub(name)
  743.                 SV*     name
  744.                 CODE:
  745.                 PUSHMARK(sp) ;
  746.                 perl_call_sv(name, G_DISCARD|G_NOARGS) ;
  747. </pre>
  748. As we are using an SV to call <I>fred</I> the following can all be used
  749. <p><pre>
  750.         CallSub("fred") ;
  751.         Callsub(\&fred) ;
  752.         $ref = \&fred ;
  753.         CallSub($ref) ;
  754.         CallSub( sub { print "Hello there\n" } ) ;
  755. </pre>
  756. As you can see, <I>perl_call_sv</I> gives you greater flexibility in how you 
  757. can specify the Perl sub.
  758. <p><h3>Example 8: Using perl_call_argv</h3>
  759. Here is a Perl sub which prints whatever parameters are passed to it.
  760. <p><pre>
  761.         sub PrintList
  762.         {
  763.             my(@list) = @_ ;
  764. </pre>
  765. <pre>
  766.             foreach (@list) { print "$_\n" }
  767.         }
  768. </pre>
  769. and here is an example of <I>perl_call_argv</I> which will call <I>PrintList</I>.
  770. <p><pre>
  771.         call_PrintList
  772.         {
  773.             dSP ;
  774.             char * words[] = {"alpha", "beta", "gamma", "delta", NULL } ;
  775. </pre>
  776. <pre>
  777.             perl_call_argv("PrintList", words, G_DISCARD) ;
  778.         }
  779. </pre>
  780. Note that it is not necessary to call <B>PUSHMARK</B> in this instance. This is
  781. because <I>perl_call_argv</I> will do it for you.
  782. <p><h3>Example 9: Using perl_call_method</h3>
  783. [This section is under construction]
  784. <p>Consider the following Perl code
  785. <p><pre>
  786.         {
  787.           package Mine ;
  788. </pre>
  789. <pre>
  790.           sub new     { bless [@_] }
  791.           sub Display { print $_[0][1], "\n" }
  792.         }
  793. </pre>
  794. <pre>
  795.         $a = new Mine ('red', 'green', 'blue') ;
  796.         call_Display($a, 'Display') ;
  797. </pre>
  798. The method <B>Display</B> just prints out the first element of the list.
  799. Here is a XSUB implementation of <I>call_Display</I>.
  800. <p><pre>
  801.         void
  802.         call_Display(ref, method)
  803.                 SV *    ref
  804.                 char *  method
  805.                 CODE:
  806.                 PUSHMARK(sp);
  807.                 XPUSHs(ref);
  808.                 PUTBACK;
  809. </pre>
  810. <pre>
  811.                 perl_call_method(method, G_DISCARD) ;
  812. </pre>
  813. <h3>Strategies for storing Context Information</h3>
  814. [This section is under construction]
  815. <p>One of the trickiest problems to overcome when designing a callback interface 
  816. is figuring 
  817. out how to store the mapping between the C callback functions and the 
  818. Perl equivalent.
  819. <p>Consider the following example.
  820. <p><h3>Alternate Stack Manipulation</h3>
  821. [This section is under construction]
  822. <p>Although I have only made use of the POP* macros to access values returned 
  823. from Perl subs, it is also possible to bypass these macros and read the 
  824. stack directly.
  825. <p>The code below is example 4 recoded to 
  826. <p><h2>SEE ALSO</h2>
  827.  
  828. <A HREF="perlapi.html">
  829. the perlapi manpage</A>
  830. <A HREF="perlguts.html">
  831. the perlguts manpage</A>
  832. <A HREF="perlembed.html">
  833. the perlembed manpage</A>
  834.  
  835. <p><h2>AUTHOR</h2>
  836. Paul Marquess <pmarquess@bfsec.bt.co.uk>
  837. <p>Special thanks to the following people who assisted in the creation of the 
  838. document.
  839. <p>Jeff Okamoto, Tim Bunce.
  840. <p><h2>DATE</h2>
  841. Version 0.4, 17th October 1994
  842. <p>